package twoD.IsoPfem.utils;

import inf.jlinalg.Vector2D;

import java.util.ArrayList;

import twoD.IsoPfem.ElasticStructure;
import twoD.IsoPfem.Node;
import twoD.IsoPfem.EmbeddedCrackModel.CrackStructure;

public class NodeUtilities {
	private static final double TOL = 1e-14;

	public static ArrayList<Node> findNode(ElasticStructure struct, double x,
			double y) {
		int cnt = 0;
		ArrayList<Node> N = new ArrayList<Node>();
		do {
			Node n = struct.getMesh().getNodes(cnt++);
			if (n.getPosition().subtract(new Vector2D(x, y)).norm2() < TOL) {
				N.add(n);
				break;
			}
		} while (cnt < struct.getMesh().getNumberOfNodes());
		return N;
	}

	public static ArrayList<Node> findNode(CrackStructure cStruct, double x,
			double y) {
		int cnt = 0;
		ArrayList<Node> N = new ArrayList<Node>();
		do {
			Node n = cStruct.getMesh().getNodes(cnt++);
			if (n.getPosition().subtract(new Vector2D(x, y)).norm2() < TOL) {
				N.add(n);
				break;
			}
		} while (cnt < cStruct.getMesh().getNumberOfNodes());
		return N;
	}

	public static ArrayList<Node> findNodeOnLine(CrackStructure cStruct,
			double a, double b, double c) {
		int cnt = 0;
		ArrayList<Node> N = new ArrayList<Node>();
		do {
			Node n = cStruct.getMesh().getNodes(cnt++);
			double x = n.getPosition().getX1();
			double y = n.getPosition().getX2();
			double f = a * x + b * y + c;
			if (Math.abs(f) < TOL)
				N.add(n);
		} while (cnt < cStruct.getMesh().getNumberOfNodes());
		return N;
	}
}
